package com.buybuyyi.stylistcrm.user.dao.impl;

import com.buybuyyi.stylistcrm.user.dao.UserProfileDao;
import com.buybuyyi.stylistcrm.user.mapper.DesignerUserProfileMapper;
import com.buybuyyi.stylistcrm.user.mapper.StylistUserProfileMapper;
import com.buybuyyi.stylistcrm.user.mapper.UserProfileMapper;
import com.buybuyyi.stylistcrm.user.po.DesignerUserProfileWithBLOBs;
import com.buybuyyi.stylistcrm.user.po.StylistUserProfileWithBLOBs;
import com.buybuyyi.stylistcrm.user.po.UserProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import stylistcrm.core.services.order.enums.EnumExceptionCode;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.security.service.SecurityService;
import stylistcrm.core.services.user.User;
import stylistcrm.core.services.user.profile.DesignerUserProfile;
import stylistcrm.core.services.user.profile.StylistUserProfile;

import java.util.Date;

/**
 * Created by yanwc on 2016/10/8.
 */
@Repository
public class UserProfileDaoImpl implements UserProfileDao {

    @Autowired
    DesignerUserProfileMapper designerUserProfileMapper;

    @Autowired
    StylistUserProfileMapper stylistUserProfileMapper;

    @Autowired
    UserProfileMapper userProfileMapper;

    @Autowired
    SecurityService securityService;

    @Override
    public User getUser(long secUserId) {
        User user = new User();
        //查找用户配置信息

        return user;
    }

    /**
     * 申请注册设计师
     * @param designer
     * @return
     * @throws BizException
     */
    @Override
    public Boolean registerDesigner(DesignerUserProfile designer) throws BizException {
        if(null==designer){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        Long userProfileId=saveUserProfile(designer);
        DesignerUserProfileWithBLOBs designerUserProfile=new DesignerUserProfileWithBLOBs();
        designerUserProfile.setBusinessLicense(designer.getBusinessLicense());
        designerUserProfile.setBusinessAddress(designer.getBusinessAddress());
        designerUserProfile.setBrandIntroduction(designer.getBrandIntroduction());
        designerUserProfile.setId(userProfileId);
        Integer ret=designerUserProfileMapper.insertSelective(designerUserProfile);
        if(ret<=0){
            throw new BizException(EnumExceptionCode.EC0004);
        }
        return Boolean.TRUE;
    }

    /**
     * 申请注册搭配师
     * @param stylist
     * @return
     * @throws BizException
     */
    @Override
    public Boolean registerStylist(StylistUserProfile stylist) throws BizException {
        if(null==stylist){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        Long userProfileId=saveUserProfile(stylist);
        StylistUserProfileWithBLOBs stylistUserProfile=new StylistUserProfileWithBLOBs();
        stylistUserProfile.setId(userProfileId);
        stylistUserProfile.setExperience(stylist.getExperience());
        stylistUserProfile.setResume(stylist.getResume());
        stylistUserProfile.setIsPartTime(stylist.isPartTime());
        Integer ret=stylistUserProfileMapper.insertSelective(stylistUserProfile);
        if(ret<=0){
            throw new BizException(EnumExceptionCode.EC0004);
        }
        return Boolean.TRUE;
    }

    private Long saveUserProfile(stylistcrm.core.services.user.profile.UserProfile userProfileDto) throws BizException {
        UserProfile userProfile=new UserProfile();
        userProfile.setNickName(userProfileDto.getNickName());
        userProfile.setRealName(userProfileDto.getRealName());
        userProfile.setHeadImageUrl(userProfileDto.getHeadImageUrl());
        userProfile.setBirthDay(userProfileDto.getBirthDay());
        userProfile.setBankAccountNumber(userProfileDto.getBankAccountNumber());
        userProfile.setSecUserId(userProfileDto.getSecUserId());
        userProfile.setGender(userProfileDto.getGender());
        userProfile.setCreateTime(new Date());
        userProfile.setLastModifyTime(new Date());
        //TODO 获取创建者id
        Integer ret=userProfileMapper.insertSelective(userProfile);
        if(ret<=0){
            throw new BizException(EnumExceptionCode.EC0004);
        }
        return userProfile.getId();
    }
}
